package M2Naming;

import java.util.ArrayList;

import javax.naming.directory.DirContext;

import lib.OperationJNDI;

import org.omg.CORBA.Object;

import M2Naming.NamingContextPackage.AlreadyBound;
import M2Naming.NamingContextPackage.CannotProceed;
import M2Naming.NamingContextPackage.InvalidName;
import M2Naming.NamingContextPackage.NotFound;



/*
 * le classe heriter le  NamingContextPOA
 * 
 */
public class NamingContextImp extends NamingContextPOA {
	private OperationJNDI op=null;
	private ArrayList<Binding> listbinding=null;
	
    public NamingContextImp()
    {
    	this.listbinding=new ArrayList<Binding>();
    	this.op=new OperationJNDI();
    	op.initJNDI();
    }


	@Override
	public void bind(NameCoponent[] n, Object obj) throws NotFound,
			CannotProceed, InvalidName, AlreadyBound {
		String bindname=n[0].id;
		DirContext ctx=op.getCtx();
		/*si le length du liste nameCoponent est eagle 0 throw le invalide name
		 * exception et le CannotProceed exception
		 */
		
		if(n.length==0)
		{
			throw new InvalidName();
		}
		/*
		 * si le name est deja existe dans le binglist throw exception
		 * et le CannotProceed exception
		 */
		for(Binding b:listbinding)
		{
			if(b.binding_name[0].id.equals(bindname)&&(b.binding==M2Naming.BindingType.nobject))
			{
				throw new AlreadyBound();
			}
		}
		//add le namecoponent a notre listbing
		Binding b=new Binding(n,M2Naming.BindingType.nobject);
		listbinding.add(b);
		// bind l'object a jndi
		op.bind(bindname, obj, ctx);
		
		System.out.println("bind resussir");
		
	}

	
	public void bind_text(NameCoponent[] n, NamingContext nc) throws NotFound,
			CannotProceed, InvalidName, AlreadyBound {
		String bindname=n[0].id;
		/*si le length du liste nameCoponent est eagle 0 throw le invalide name
		 * exception et le CannotProceed exception
		 */
		if(n.length==0)
		{
			throw new InvalidName();
		}
		/*
		 * si le name est deja existe dans le binglist throw exception
		 * et le CannotProceed exception
		 */
		for(Binding b:listbinding)
		{
			if(b.binding_name[0].id.equals(bindname)&&(b.binding==M2Naming.BindingType.ncontext))
			{
				throw new AlreadyBound();
			}
		}
		//add le namecoponent a notre listbing
		Binding b=new Binding(n,M2Naming.BindingType.ncontext);
		listbinding.add(b);
		op.bind(bindname,op.getCtx());
		System.out.println("bind resussir");
	}

	/*
	 * (non-Javadoc)
	 * @see M2Naming.NamingContextOperations#new_contxt()
	 */
	public NamingContext new_contxt() {
		
		NamingContext nct=null;
		NamingContextFils nci=new NamingContextFils();
		//System.out.println("ok");
		try 
		{
			org.omg.CORBA.Object vb=(org.omg.CORBA.Object)nci;
			System.out.println("ok");
		}
		catch(org.omg.CORBA.BAD_PARAM ex)
		{
			//ex.printStackTrace();
			System.out.println("casting faulte");
		}
		return (NamingContext)nci;
	}

	public Object resolve(NameCoponent[] n) throws NotFound, CannotProceed,
			InvalidName {
		String bindname=n[0].id;
		boolean notfound=true;
		if(n.length==0)
		{
			throw new InvalidName();
		}
		for(Binding b:listbinding)
		{
			if(b.binding_name[0].id.equals(bindname)&&(b.binding==M2Naming.BindingType.nobject))
			{
				notfound=false;
			}
		}
		if(notfound)
		{
			throw new NotFound();
		}
		Object o=(Object) op.resolve(bindname);
		return o;
	}
	
	public void unbind(NameCoponent[] n) throws NotFound, CannotProceed,
			InvalidName {
		String bindname=n[0].id;
		boolean notfound=true;
		
		if(n.length==0)
		{
			throw new InvalidName();
		}
		for(Binding b:listbinding)
		{
			if(b.binding_name[0].id.equals(bindname))
			{
				listbinding.remove(b);
				notfound=false;
			}
		}
		if(notfound)
		{
			throw new NotFound();
		}
		op.unbind(bindname);
	}

	public ArrayList<Binding> getListbinding() {
		return listbinding;
	}


	public void setListbinding(ArrayList<Binding> listbinding) {
		this.listbinding = listbinding;
	}
}
